using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;

public partial class Admin_DonHangUpdate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
            LoadDropDownList();
        }
    }
    private void LoadDropDownList()
    {
        tbKhachHangCollection khCollection = tbKhachHangBUS.GetAll();
        ddl_khachhang.DataTextField = tbKhachHangColumns.HoTen.ToString();
        ddl_khachhang.DataValueField = tbKhachHangColumns.MaKH.ToString();
        ddl_khachhang.DataSource = khCollection;
        ddl_khachhang.DataBind();
        ddl_khachhang.Items.Insert(0, new ListItem("All", "0"));

        tbTinhTrangThanhToanCollection ttttCollection = tbTinhTrangThanhToanBUS.GetAll();
        ddl_tinhtrangthanhtoan.DataTextField = tbTinhTrangThanhToanColumns.TenTTTT.ToString();
        ddl_tinhtrangthanhtoan.DataValueField = tbTinhTrangThanhToanColumns.MaTTTT.ToString();
        ddl_tinhtrangthanhtoan.DataSource = ttttCollection;
        ddl_tinhtrangthanhtoan.DataBind();
        ddl_tinhtrangthanhtoan.Items.Insert(0, new ListItem("All", "0"));

        tbTinhTrangGHCollection ttghCollection = tbTinhTrangGHBUS.GetAll();
        ddl_tinhtranggiaohang.DataTextField = tbTinhTrangGHColumns.TenTTGH.ToString();
        ddl_tinhtranggiaohang.DataValueField = tbTinhTrangGHColumns.MaTTGH.ToString();
        ddl_tinhtranggiaohang.DataSource = ttghCollection;
        ddl_tinhtranggiaohang.DataBind();
        ddl_tinhtranggiaohang.Items.Insert(0, new ListItem("All", "0"));

        tbPhuongThucThanhToanCollection ptttCollection = tbPhuongThucThanhToanBUS.GetAll();
        ddl_phuongthucthanhtoan.DataTextField = tbPhuongThucThanhToanColumns.TenPTTT.ToString();
        ddl_phuongthucthanhtoan.DataValueField = tbPhuongThucThanhToanColumns.MaPTTT.ToString();
        ddl_phuongthucthanhtoan.DataSource = ptttCollection;
        ddl_phuongthucthanhtoan.DataBind();
        ddl_phuongthucthanhtoan.Items.Insert(0, new ListItem("All", "0"));

        tbNhanVienCollection nvCollection = tbNhanVienBUS.GetAll();
        ddl_nhanviengiaohang.DataTextField = tbNhanVienColumns.TenNV.ToString();
        ddl_nhanviengiaohang.DataValueField = tbNhanVienColumns.MaNV.ToString();
        ddl_nhanviengiaohang.DataSource = nvCollection;
        ddl_nhanviengiaohang.DataBind();
        ddl_nhanviengiaohang.Items.Insert(0, new ListItem("All", "0"));

        tbDiaDiemCollection ddCollection = tbDiaDiemBUS.GetAll();
        ddl_diachigiaohang.DataTextField = tbDiaDiemColumns.DiaChi.ToString();
        ddl_diachigiaohang.DataValueField = tbDiaDiemColumns.MaDD.ToString();
        ddl_diachigiaohang.DataSource = ddCollection;
        ddl_diachigiaohang.DataBind();
        ddl_diachigiaohang.Items.Insert(0, new ListItem("All", "0"));
    }
    private void LoadData()
    {
        try
        {
            int iD=int.Parse (Request .QueryString ["iD"].ToString ());
            txt_madonhang .Text =iD.ToString ();
            tbDonHang donhang=tbDonHangBUS .GetByID (iD );
            ddl_khachhang .SelectedValue =donhang .KhachHang .ToString ();
            txt_ngaydathang .Text =donhang .NgayDatHang .ToShortDateString  ();
            txt_ngaygiaohang .Text=donhang .NgayGiaoHang.ToShortDateString ();
            txt_trigia .Text =donhang .TriGia .ToString ();
            ddl_tinhtranggiaohang .SelectedValue =donhang .TinhTrangGH .ToString ();
            ddl_phuongthucthanhtoan .SelectedValue =donhang .PhuongThucThanhToan .ToString ();
            ddl_tinhtrangthanhtoan .SelectedValue =donhang.TinhTrangThanhToan .ToString ();
            ddl_nhanviengiaohang .SelectedValue =donhang .NhanVienGiaoHang .ToString ();
            ddl_diachigiaohang .SelectedValue =donhang .DiaChiGH .ToString ();
        }
        catch
        {
            Response .Redirect ("~/Admin/DonHangDanhSach.aspx");
        }

    }
    protected void btn_sua_Click(object sender, EventArgs e)
    {
        try
        {
            int iD=int.Parse (Request .QueryString ["iD"].ToString ());
            tbDonHang donhang=tbDonHangBUS .GetByID (iD);
            donhang.KhachHang =int.Parse (ddl_khachhang .SelectedValue );
            donhang.NgayDatHang =DateTime .Parse (txt_ngaydathang .Text );
            donhang.NgayGiaoHang =DateTime .Parse (txt_ngaygiaohang .Text );
            donhang .TriGia =int.Parse (txt_trigia .Text );
            donhang .TinhTrangGH =int.Parse (ddl_tinhtranggiaohang .SelectedValue );
            donhang .PhuongThucThanhToan =int.Parse (ddl_phuongthucthanhtoan .SelectedValue );
            donhang .TinhTrangThanhToan =int.Parse (ddl_tinhtrangthanhtoan .SelectedValue );
            donhang .NhanVienGiaoHang =int.Parse (ddl_nhanviengiaohang .SelectedValue );
            donhang .DiaChiGH =int.Parse (ddl_diachigiaohang .SelectedValue );
            tbDonHangBUS .Update (donhang );
        }
        catch
        {
        }
        Response .Redirect ("~/Admin/DonHangDanhSach.aspx");
        Response .Redirect (Request .RawUrl );
    }
}
